package com.baizhi.service.impl;

import com.baizhi.dao.UserDAO;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.util.GenerateUserUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserDAO userDAO;

    @Override
    public PageInfo<User> showAll(int page,int limit,String username) {
        PageHelper.startPage(page,limit);
        List<User> users = userDAO.selectAll(username);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public void updateState(int id, int userStatus) {
        userDAO.updateUserState(id, userStatus);
    }

    @Override
    public List<Map> showBySex() {
        return userDAO.selectBySex();
    }

    @Override
    public List<Map> showByTime(int start,int end) {
        return userDAO.selectByTime(start, end);
    }

    @Override
    public List<Map> showByProvince() {
        return userDAO.selectByProvince();
    }

    @Override
    public void addMany() {
        System.out.println("=====");
        List<User> list = new ArrayList<User>();
        for (int i=0;i<1000;i++){
            list.add(GenerateUserUtil.getUser());
        }
        userDAO.insertMany(list);
        System.out.println("添加成功");
    }
}
